package org.rc.task;

import java.util.Map;

/**
 * Base class for an appengine task to submit
 *
 * @author dokeeffe
 */
public abstract class Task {

    /**
     * A default processing limit of 25 seconds
     */
    protected static final long LIMIT_MILLIS = 1000 * 25;

    /**
     * Run the task. Impltment this in concrete classes.
     * @param parameterMap
     */
    public abstract void run(Map parameterMap);

    /**
     * Returns the UI friendly name for the task
     * @return
     */
    public abstract String getUiName();

    /**
     * Get the name of the task.
     * @return
     */
    public String getTaskName() {
        return this.getClass().getSimpleName();
    }

    /**
     * Get a parameter from the parameterMap by name where the map contains an array of values.
     * Return the first one or null.
     * @param parameterMap
     * @param name
     * @return
     */
    protected String getFirstArrayParam(Map parameterMap, String name) {
        String[] value = (String[]) parameterMap.get(name);
        String result = null;
        if(value!=null) {
            if(value.length>1) {
                throw new IllegalArgumentException("Cannot pass more than once value");
            } else if(value.length==1){
                result = value[0];
            }
        }
        return result;
    }
}
